package com.xywh.micro_warehousing.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xw.repo.XEditText;
import com.xywh.micro_warehousing.R;
import com.xywh.micro_warehousing.di.component.DaggerModifyPwdComponent;
import com.xywh.micro_warehousing.di.module.ModifyPwdModule;
import com.xywh.micro_warehousing.mvp.contract.ModifyPwdContract;
import com.xywh.micro_warehousing.mvp.presenter.ModifyPwdPresenter;
import com.xywh.micro_warehousing.mvp.ui.entity.ModifyPwd;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ACTIVITY_MODIFY_PWD)
public class ModifyPwdActivity extends BaseActivity<ModifyPwdPresenter> implements ModifyPwdContract.View {


    @BindView(R.id.xet_old_password)
    XEditText xetOldPassword;
    @BindView(R.id.xet_new_password)
    XEditText xetNewPassword;
    @BindView(R.id.xet_new_password_again)
    XEditText xetNewPasswordAgain;
    @BindView(R.id.btn_save)
    Button btnSave;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerModifyPwdComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .modifyPwdModule(new ModifyPwdModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_modify_pwd; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_modify_pwd));
//        getModifyPwdData();
    }

    private void getModifyPwdData() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getModifyPwdData(map);
    }

    @Override
    public void getModifyPwdDataSuccess(ModifyPwd entityData) {

        if (entityData != null && entityData.getCode() == 200) {
            mSharedPreferencesHelper.putString(BaseConstants.PASSWORD, getNewPassword());
            showMessage("密码修改成功");
            killMyself();
        } else {
            showMessage("密码修改失败");
        }


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick(R.id.btn_save)
    public void onViewClicked() {

        if (TextUtils.isEmpty(getOldPassword())) {
            showMessage("请输入旧密码");
            return;
        }
        if (TextUtils.isEmpty(getNewPassword())) {
            showMessage("请输入新密码");
            return;
        }
        if (TextUtils.isEmpty(getNewPasswordAgain())) {
            showMessage("请输入确认密码");
            return;
        }
        if (!getNewPassword().equals(getNewPasswordAgain())) {
            showMessage("两次密码输入不一致");
            return;
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("oldPassword", getOldPassword());
        map.put("newPassword", getNewPassword());
        mPresenter.getModifyPwdData(map);
    }

    @NotNull
    private String getOldPassword() {
        return xetOldPassword.getText().toString().trim();
    }

    @NotNull
    private String getNewPassword() {
        return xetNewPassword.getText().toString().trim();
    }

    @NotNull
    private String getNewPasswordAgain() {
        return xetNewPasswordAgain.getText().toString().trim();
    }
}
